package test_autoencode;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Main class for a test auto-encoding program.
 * @author Raiscan
 */
public class Main {
    //An enum for the flags. Not used yet.
    private enum Flags { NOT_STARTED, CAPTURING, ENC_VIDEO, ENC_AUDIO, MUX,
                         DIST, FINISHED, ERR_CAP, ERR_VID, ERR_AUD, ERR_MUX,
                         ERR_DIST};
    private Properties config = new Properties();
    private String fceuxPath, x264Path, vorbisPath, mencoderPath, mplayerPath,
                   mkvmergePath, movieFile, romName;
    private int endGame;
    private Flags state;
    private boolean success;
    
    public Main()
    {
        try
        {
            config.load(new FileInputStream("config.properties"));
        }
        catch (IOException e)
        {
            System.err.println("No properties file, aborting");
            e.printStackTrace();
            System.exit(1);
        }
        fceuxPath = config.getProperty("FCEUXPath");
        x264Path = config.getProperty("x264Path");
        mencoderPath = config.getProperty("mencoderPath");
        mplayerPath = config.getProperty("mplayerPath");
        vorbisPath = config.getProperty("vorbisPath");
        movieFile = config.getProperty("movieFile");
        mkvmergePath = config.getProperty("mkvmergePath");
        //TODO: tasape will need to do romName matching on the provided ROM name.
        romName = config.getProperty("romName");
        endGame = Integer.parseInt(config.getProperty("endGame"));

        state = Flags.CAPTURING;
        Emulator emulator = new Emulator(fceuxPath, movieFile, romName, endGame);
        success = emulator.runEmulator();
        if (!success)
        {
            state = Flags.ERR_CAP;
            System.err.println("Failure during capture");
            System.exit(state.ordinal());
        }
        state = Flags.ENC_VIDEO;
        Encoder encoder = new Encoder(mencoderPath, mplayerPath, x264Path,
                                      vorbisPath, mkvmergePath, movieFile, romName, endGame);
        success = encoder.mixLogo() && encoder.encodeVideo();
        if (!success)
        {
            state = Flags.ERR_VID;
            System.err.println("Failure during video encoding");
            System.exit(state.ordinal());
        }
        state = Flags.ENC_AUDIO;
        success = encoder.encodeAudio();
         if (!success)
        {
            state = Flags.ERR_AUD;
            System.err.println("Failure during audio encoding");
            System.exit(state.ordinal());
        }
        //TODO: Muxing
        state = Flags.MUX;
        success = encoder.mux();
          if (!success)
        {
            state = Flags.ERR_MUX;
            System.err.println("Failure during muxing");
            System.exit(state.ordinal());
        }
        state = Flags.FINISHED;
    }



    /**
     * main method for entry to auto-encoder.
     * @param args
     */
    public static void main(String[] args)
    {
        new Main();
    }
}
